﻿package mortal.game.view.achievement
{
    import Message.Public.*;
    import extend.language.*;
    import flash.events.*;
    import mortal.common.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.view.achievement.data.*;
    import mortal.game.view.achievement.panel.*;
    import mortal.game.view.common.*;
    import mortal.game.view.copy.QuestionCopy.data.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class AchievementController extends Controller
    {
        private var _achiModule:AchievementModule;
        private var _achiCache:AchievementCache;
        private var _achiProx:AchievementProx;
        private var _tipPanel:AchievementRewardTipPanel;

        public function AchievementController()
        {
            this._achiCache = Cache.instance.achievent;
            this._achiProx = GameProxy.achievement;
            return;
        }// end function

        override protected function initView() : IView
        {
            if (this._achiModule == null)
            {
                this._achiModule = new AchievementModule();
                this._achiModule.addEventListener(WindowEvent.SHOW, this.onWinShow);
                this._achiModule.addEventListener(WindowEvent.CLOSE, this.onWinClose);
            }
            return this._achiModule;
        }// end function

        override protected function initServer() : void
        {
            EventUtil.addSelfEvent(EventName.AchievementOpenPageByOther, this.onOpenModule, true);
            EventUtil.addNetEvent(ServerCommand.AchievementCategoryInfoUpdate, this.onCategoryUpdateHd, true);
            EventUtil.addNetEvent(ServerCommand.AchievementTypeInfoUpdate, this.onTypeUpdateHd, true);
            EventUtil.addSelfEvent(EventName.StageResize, this.onStageResize, true);
            EventUtil.addSelfEvent(EventName.AchievementTipPanelCk, this.turnToPageTip, true);
            EventUtil.addNetEvent(ServerCommand.AchievementUpdateCanGetRewardFlag, this.onHasRewardHd, true);
            LevelCheckUtil.addInit(this.onHasRewardHd);
            return;
        }// end function

        private function addEvnts(param1:Boolean) : void
        {
            EventUtil.addSelfEvent(EventName.AchievementOnOpenPage, this.onOpenPageHd, param1);
            EventUtil.addNetEvent(ServerCommand.AchievementPointUpdate, this.onUpdataTotalPoint, param1);
            EventUtil.addNetEvent(ServerCommand.AchievementCategoryDataGot, this.onInitCategoryHd, param1);
            EventUtil.addNetEvent(ServerCommand.AchievementUpComingDataGot, this.onInitUpComingHd, param1);
            EventUtil.addSelfEvent(EventName.AchievementCategorySelect, this.onQueryTypeDataHd, param1);
            EventUtil.addNetEvent(ServerCommand.AchievementTypeDataGot, this.tabCkBackHd, param1);
            EventUtil.addSelfEvent(EventName.AchievementGetCategoryBonus, this.ongetCategoryRewardHd, param1);
            EventUtil.addSelfEvent(EventName.AchievementGetTypBonus, this.ongetTypeRewardHd, param1);
            EventUtil.addSelfEvent(EventName.AchievementUpcomingBtnCk, this.onUpcomingCk, param1);
            return;
        }// end function

        private function initHd(param1:Object) : void
        {
            return;
        }// end function

        private function onOpenModule(event:DataEvent) : void
        {
            if (LevelCheckUtil.isLevelEnougth(57) == false)
            {
                MsgManager.showRollTipsMsg("" + Language.getString(70812));
                return;
            }
            this.view.show();
            return;
        }// end function

        private function onWinShow(event:Event) : void
        {
            this.addEvnts(true);
            if (this._achiModule && this._achiModule.uiLoadComplete)
            {
                this.onOpenPageHd(null);
            }
            return;
        }// end function

        private function onOpenPageHd(event:DataEvent) : void
        {
            this._achiProx.getAchievementOverview();
            this._achiProx.getUpcomingAchievement();
            return;
        }// end function

        private function onWinClose(event:Event) : void
        {
            this.addEvnts(false);
            return;
        }// end function

        private function onUpdataTotalPoint(param1:Object) : void
        {
            if (this._achiModule)
            {
                this._achiModule.curPanel.updateTotalPoint(param1);
            }
            return;
        }// end function

        private function onInitCategoryHd(param1:Object) : void
        {
            if (this._achiModule)
            {
                this._achiModule.curPanel.initCategory();
            }
            return;
        }// end function

        private function onQueryTypeDataHd(event:DataEvent) : void
        {
            var _loc_2:* = int(event.data);
            if (_loc_2 == 0)
            {
                this.onOpenPageHd(null);
            }
            else
            {
                this._achiProx.getAchievementCategroy(_loc_2);
            }
            return;
        }// end function

        private function onInitUpComingHd(param1:Object) : void
        {
            if (this._achiModule)
            {
                this._achiModule.curPanel.initUpComing();
            }
            return;
        }// end function

        private function tabCkBackHd(param1:Object) : void
        {
            if (this._achiModule)
            {
                this._achiModule.curPanel.updateAfterTabCk();
            }
            return;
        }// end function

        private function ongetCategoryRewardHd(event:DataEvent) : void
        {
            var _loc_2:* = event.data as int;
            if (_loc_2 == 0)
            {
                return;
            }
            Cache.instance.achievent.getCatoryType = _loc_2;
            this._achiProx.getCategoryAchievementReward(_loc_2);
            return;
        }// end function

        private function ongetTypeRewardHd(event:DataEvent) : void
        {
            var _loc_2:* = event.data as int;
            if (_loc_2 == 0)
            {
                return;
            }
            Cache.instance.achievent.getTypeType = _loc_2;
            this._achiProx.getAchievementReward(_loc_2);
            return;
        }// end function

        private function onUpcomingCk(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SAchievementType;
            this.queryByType(_loc_2);
            return;
        }// end function

        private function queryByType(param1:SAchievementType) : void
        {
            if (param1 == null)
            {
                return;
            }
            if (this._achiModule)
            {
                this._achiModule.curPanel.openTabByIndex(param1.category, param1.type);
            }
            return;
        }// end function

        private function onCategoryUpdateHd(param1:Object) : void
        {
            var _loc_2:SAchievementCategory = null;
            this.showTipPanel(param1);
            if (this.view.isHide == false)
            {
                _loc_2 = param1 as SAchievementCategory;
                this._achiModule.curPanel.updateCategory(_loc_2);
            }
            return;
        }// end function

        private function showTipPanel(param1:Object) : void
        {
            if (this.view.isHide)
            {
                if (Cache.instance.achievent.checkHasReward(param1) && AchievementUtil.checkOpenLevel())
                {
                }
            }
            return;
        }// end function

        private function onTypeUpdateHd(param1:Object) : void
        {
            var _loc_2:SAchievementType = null;
            this.showTipPanel(param1);
            if (this._achiModule && this._achiModule.isHide == false)
            {
                _loc_2 = param1 as SAchievementType;
                this._achiModule.curPanel.updateType(_loc_2);
            }
            return;
        }// end function

        public function getTipPanel() : AchievementRewardTipPanel
        {
            if (this._tipPanel == null)
            {
                this._tipPanel = UIFactory.getUICompoment(AchievementRewardTipPanel);
            }
            return this._tipPanel;
        }// end function

        private function turnToPageTip(event:DataEvent) : void
        {
            (this.view as AchievementModule).openPageByParam(event.data);
            if (this._tipPanel)
            {
                this._tipPanel.dispose();
                this._tipPanel = null;
            }
            return;
        }// end function

        private function onHasRewardHd(param1:Object) : void
        {
            var _loc_2:* = Cache.instance.achievent.canGetFlagArr;
            if (_loc_2 == null)
            {
                return;
            }
            var _loc_3:* = _loc_2 != null && _loc_2.length > 0;
            GameController.mapNavBar.rightTopBar.blinkBtn("achievement", _loc_3);
            if (this._achiModule && this._achiModule.isHide == false)
            {
                this._achiModule.curPanel.updateCanGetRw();
            }
            return;
        }// end function

        private function onStageResize(event:DataEvent) : void
        {
            if (this._tipPanel)
            {
                this._tipPanel.onStageResize();
            }
            return;
        }// end function

        private function testTip(param1:Object) : void
        {
            this.getTipPanel().show(null);
            return;
        }// end function

    }
}
